﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Nhom14_HTTTHD_BuyMe
{
    public partial class detaildomain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // demo session
            button_make_offer.Text = "Closed";
            button_make_offer.NavigateUrl = "~/bid.aspx?id=" + Request.QueryString["id"] + "&type=2";

            addtowatch.CommandArgument =   Request.QueryString["id"];

            // them vào danh sách domain đang xem

            int id = Convert.ToInt32(Request.QueryString["id"]);
            using (websitebrokenEntities context = new websitebrokenEntities())
            {
                domain w = (from x in context.domains
                             where x.id == id
                             select x).FirstOrDefault();
                if (w != null)
                {
                    int sts = (int)w.status;
                    if (sts == 0)
                    {
                        button_make_offer.CssClass = "";
                        button_make_offer.Text = "<img src=\"Images/closed.jpg\" />";
                        button_make_offer.ForeColor = System.Drawing.Color.Orange;
                        button_make_offer.Enabled = false;
                       
                    }
                    else
                    {
                        Label2.Text = "<img src=\"Images/domain-fo-sale.gif\" width=\"180px\"/>";
                        button_make_offer.Enabled = true;
                    }
                }
            }

        }

        // tạo giao dịch
        protected void btnLogBid_Click(object sender, EventArgs e)
        {
            // kiem tra xem user này có phải là chủ nhân của website này  mới xác nhận
            // id username 
            int idusename = int.Parse(Session["user"].ToString());
            int idWebsite = int.Parse(Request.QueryString["id"]);


            //lấy id_bid
            Button btn = (Button)sender;
            int id_bid = int.Parse(btn.CommandArgument.ToString());

            // type_bid là domain
            // goi webserive tạo giao dịch

            // SID
            string par1 = "VU";
            // bid website hay domain
            int par2 = id_bid;
            int par3 = 2;

            string address = "http://localhost:49744/WebService1.asmx";
            string function = "createTransaction";

            int res = (int)WebserviceInvoker.WSInvoker.CallWebService(address, "WebService1", function, new object[] { par1, par2, par3 });

            // goi webserive gửi tin nhắn tới cả 2 đối tượng
            // tới bán _ mua
            /*string function1 = "messageTransaction";
            int type_b = 2;
            string mess = "Bib tin đăng http://localhost:49744/detaildomain.aspx?id=" + idWebsite.ToString() + ", vui lòng check lại";
            int res1 = (int)WebserviceInvoker.WSInvoker.CallWebService(address, "WebService1", function1, new object[] { idusename, par2, type_b, mess });
            */
        }

        // them vao danh sach xem domain
        protected void addtowatch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            int i = int.Parse(btn.CommandArgument);

            List<int> ld = (List<int>)Session["dlist"];
            ld.Add(i);

            Session["dlist"] = ld;
        }

        protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
        {

            // lay user logged
            if (Session["user"] == null)
            {
                int id = Convert.ToInt32(Session["user"]);
                Button btn = (Button)e.Item.FindControl("btnLogBid");

                btn.Visible = false;
            }
            else
            {

                int id = Convert.ToInt32(Session["user"]);
                Button btn = (Button)e.Item.FindControl("btnLogBid");
                int idbtn = int.Parse(btn.CommandArgument);
                TextBox a = (TextBox)e.Item.FindControl("TextBox1");

                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    bid_domain t = (from x in context.bid_domain
                                     where x.id == idbtn
                                     select x).FirstOrDefault();
  

                    if (t != null)
                    {
                        int idw = (int)t.id_domain;
                        using (websitebrokenEntities contextsub = new websitebrokenEntities())
                        {
                            domain w = (from x in context.domains
                                         where x.id == idw
                                         select x).FirstOrDefault();
                            if (w != null)
                            {
                                int idu = (int)w.id_account;
                                if (idu == id)
                                {
                                    btn.Visible = true;
                                }
                                else
                                {
                                    btn.Visible = false;
                                }
                            }
                        }
                    }

                }



            }
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
             int id = Convert.ToInt32(Request.QueryString["id"]);
             using (websitebrokenEntities context = new websitebrokenEntities())
             {
                 domain w = (from x in context.domains
                             where x.id == id
                             select x).FirstOrDefault();
                 if (w != null)
                 {
                     int sts = (int)w.id_type;
                     if (sts == 1)
                     {
                         Label l = (Label)e.Item.FindControl("premiere");
                         l.Visible = true;
                     }

                 }
             }
        }
    }
}